package com.atgouwu.mall.cart.controlller;

import com.atgouwu.mall.cart.service.CartService;
import com.atgouwu.mall.cart.vo.CartItemVo;
import com.atgouwu.mall.cart.vo.CartVo;
import com.atgouwu.mall.common.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * 购物车
 */
@Api(tags = "购物车")
@RestController
@RequestMapping("cart/data")
public class CartController {

    @Autowired
    private CartService cartService;

    //获取购物车(登录购物车+临时购物车)
    @ApiOperation(value = "获取购物车", notes = "获取购物车(登录购物车+临时购物车)")
    @GetMapping(value = "/api/getCarts")
    public R getCarts() throws ExecutionException, InterruptedException {
        CartVo cartVo = cartService.getCart();
        return R.ok().put("data", cartVo);
    }

    //添加商品到购物车redis
    @ApiOperation(value = "添加商品到购物车", notes = "添加商品到购物车redis")
    @GetMapping(value = "/api/addCartItem")
    public R addCartItem(@ApiParam(value = "skuId", example = "1", required = true) @RequestParam("skuId") Long skuId, @ApiParam(value = "加入数量", example = "1", required = true) @RequestParam("num") Integer num) throws ExecutionException, InterruptedException {
        cartService.addToCart(skuId, num);
        return R.ok();
    }

    //修改购物项数量
    @GetMapping(value = "/countItem")
    public R countItem(@RequestParam(value = "skuId") Long skuId, @RequestParam(value = "num") Integer num) {
        cartService.changeItemCount(skuId, num);
        return R.ok();
    }

    //删除购物项
    @GetMapping(value = "/deleteItem")
    public R deleteItem(@RequestParam("skuId") Integer skuId) {
        cartService.deleteIdCartInfo(skuId);
        return R.ok();
    }

    //查询当前用户购物车选中的商品项
    @GetMapping(value = "/currentUserCartItems")
    @ResponseBody
    public List<CartItemVo> getCurrentCartItems(@RequestParam("token") String token) {
        List<CartItemVo> cartItemVoList = cartService.getUserCartItems();
        return cartItemVoList;
    }

}
